package com.smart.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smart.model.AccessToken;
import com.smart.model.Admin;
import com.smart.model.House;
import com.smart.model.User;
import com.smart.model.template.ArrearData;
import com.smart.model.template.ArrearTemplate;
import com.smart.model.template.Parameter;
import com.smart.service.HouseService;
import com.smart.service.PropertyManagementFeeService;
import com.smart.service.UserPaymentRecordService;
import com.smart.service.UserService;
import com.smart.util.WeixinUtil;

@Controller
public class ArrearInformServlet extends HttpServlet {
	@RequestMapping(value="/pc/arrear", method = RequestMethod.POST)
    public void arrear(HttpServletRequest req, HttpServletResponse resp)
    {
		HttpSession session=req.getSession();
		Admin admin=(Admin)session.getAttribute("admin");
		int communityId=admin.getCommunityId();
		boolean success=true;
		PrintWriter out=null;
    	System.out.println("arrear method");
    	//获取催缴的年份
    	String year=req.getParameter("year");
    	//type,1表示物业费，2表示车位费
    	String type=req.getParameter("type");
    	System.out.println("year:"+year+",type:"+type);
    	AccessToken token = WeixinUtil.getAccessToken();
    	UserService userSer=new UserService();
    	try {
			List<User> uList=userSer.selectUser(communityId);
			for(User u:uList)
			{
				String open_id=u.getOpenID();
				String name=u.getUserName();
				int house_id=u.getHouseID();//获取住宅号
				System.out.println("house_id:"+house_id);
				int verified=u.getVerified();
				//通过住宅号获取房间的而其他信息
				HouseService houseSer=new HouseService();
				House house=houseSer.getHouse(house_id);
				System.out.println("house_id:"+house.getId()+",building_id:+"+house.getBuilding_id()+",room_no:"+house.getRoom_no()+",area:"+house.getArea());
				ArrearTemplate arrearTemplate=new ArrearTemplate();
				arrearTemplate.setTemplate_id("eoFqPRLs7DLYOAetkfr8uQAU9PM40seNFZLDZlVWWxY");
				arrearTemplate.setTouser(open_id);
				if(open_id.equals("fw")) continue;
				//判断是否欠费
				UserPaymentRecordService userPaymentRecordService=new UserPaymentRecordService();
				int isPayed=userPaymentRecordService.isPayed(year, house_id);
				//如果已经交过了，就不用交了
				if(isPayed==1) continue;
				
				arrearTemplate.setUrl("");
				ArrearData arrearData=new ArrearData();
				Parameter address=new Parameter();
				Parameter first=new Parameter();
				Parameter pay=new Parameter();
				Parameter userName=new Parameter();
				Parameter remark=new Parameter();
				address.setValue("第"+house.getBuilding_id()+"幢，房间号："+house.getRoom_no());
				first.setValue("您好，您需缴纳的物业费如下所示：");
				userName.setValue(u.getUserName());
				//获取应该交的费用
				PropertyManagementFeeService propertyManagementFeeService=new PropertyManagementFeeService();
			    double fee=propertyManagementFeeService.getFee(house_id);
			    pay.setValue(""+fee);
				remark.setValue("请尽快缴纳！！！");
				arrearData.setAddress(address);
				arrearData.setFirst(first);
				arrearData.setRemark(remark);
				arrearData.setUserName(userName);
				arrearData.setPay(pay);
				arrearTemplate.setData(arrearData);
				String message = JSONObject.fromObject(arrearTemplate).toString();
				System.out.println(message);
				WeixinUtil.sendTemplateMessage(token, message);
			}
			out=resp.getWriter();
			out.print(success);
			System.out.println(success);
			out.flush();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally
    	{
    		if(out!=null) out.close();
    	}
    	
	}

}
